﻿// -----------------------------------------------------------------------
// <copyright file="QuanHuyen.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.ENTITY
{
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    
    /// <summary>
    /// Lớp quận, huyện
    /// </summary>
    public class QuanHuyen
    {
        /// <summary>
        /// Gets or sets Id quận, huyện
        /// </summary>
        public int IdQuanHuyen { get; set; }

        /// <summary>
        /// Gets or sets Id tỉnh thành
        /// </summary>
        public int IdTinhThanh { get; set; }

        /// <summary>
        /// Gets or sets Mã quận, huyện
        /// </summary>
        public string MaQuanHuyen { get; set; }

        /// <summary>
        /// Gets or sets Tên quận, huyện
        /// </summary>
        public string TenQuanHuyen { get; set; }

        /// <summary>
        /// Gets tỉnh thành
        /// </summary>
        public TinhThanh TinhThanh 
        {
            get 
            {
                try
                {
                    TinhThanh tinhthanh = null;
                    SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneTinhThanhGetById, this.IdQuanHuyen);

                    while (reader.Read())
                    {
                        tinhthanh = new TinhThanh();
                        if (!reader.IsDBNull(0))
                        {
                            tinhthanh.IdTinhThanh = reader.GetInt32(0);
                        }

                        if (reader.GetString(1) != null)
                        {
                            tinhthanh.MaTinhThanh = reader.GetString(1);
                        }

                        if (reader.GetString(2) != null)
                        {
                            tinhthanh.TenTinhThanh = reader.GetString(2);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return tinhthanh;
                }
                catch (SqlException ex)
                {
                    LogHelper.Instance.WriteError(ex);
                    throw;
                }
            }
        }
        
        /// <summary>
        /// Gets Phường, Xã
        /// </summary>
        public IList<PhuongXa> PhuongXas
        {
            get
            {
                try
                {
                    IList<PhuongXa> lstPhuongXa = new List<PhuongXa>();
                    var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePhuongXaGetById, this.IdQuanHuyen);
                    while (reader.Read())
                    {
                        PhuongXa phuongXa = new PhuongXa();
                        if (!reader.IsDBNull(0))
                        {
                            phuongXa.IdPhuongXa = reader.GetInt32(0);
                        }

                        if (reader.IsDBNull(1))
                        {
                            phuongXa.IdQuanHuyen = reader.GetInt32(1);
                        }

                        if (reader.GetString(2) != null)
                        {
                            phuongXa.MaPhuongXa = reader.GetString(2);
                        }

                        if (reader.GetString(3) != null)
                        {
                            phuongXa.TenPhuongXa = reader.GetString(3);
                        }

                        lstPhuongXa.Add(phuongXa);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstPhuongXa;
                }
                catch (SqlException ex)
                {
                    LogHelper.Instance.WriteError(ex);
                    throw;
                }
            }
        }
    }
}